package com.woniu.finance.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.finance.entity.Bill;
import com.woniu.finance.entity.RequestParam;
import com.woniu.finance.mapper.BillMapper;
import com.woniu.finance.service.IBillService;
import com.woniu.util.utils.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BillServiceImpl implements IBillService {
    @Autowired(required = false)
    private BillMapper billMapper;

    @Override
    public HttpResult<?> findAllBill() {
        List<Bill> bills = billMapper.selectList(null);
        try {
            return new HttpResult<>().ok(bills);
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    @Override
    public HttpResult<?> saveBill(Bill bill) {
        try {
            billMapper.insert(bill);
            return new HttpResult<>().ok();
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    @Override
    public HttpResult<?> modifyBill(Bill bill) {
        try {
            billMapper.updateById(bill);
            return new HttpResult<>().ok();
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    /** 
    * @Description: 根据id删
    * @Param: [id]
    * @return: com.woniu.util.utils.HttpResult<?>
    * @Author: 阿水
    * @Date: 2023/5/8
    */
    
    @Override
    public HttpResult<?> removeBillById(Integer id) {
        try {
            billMapper.deleteById(id);
            return new HttpResult<>().ok();
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    @Override
    public HttpResult<?> findBillById(Integer id) {
        try {
            Bill bill = billMapper.selectById(id);
            return new HttpResult<>().ok(bill);
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    @Override
    public HttpResult<?> findBillByEnterpriseId(RequestParam<Bill> param) {
        //分页开始
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        //调用mapper层接口
        List<Bill> billList = billMapper.findBillByEnterpriseId(param.getData().getEnterpriseId());
        //把mapper接口返回参数放入pageInfo
        PageInfo pageInfo = new PageInfo(billList);

        return  new HttpResult<>().ok(pageInfo);
    }

}
